# Maintainer: Jannick <thirdedition@gmx.net>

_realname=igraph
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.10.15
pkgrel=1
pkgdesc="Library for the analysis of networks (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://igraph.org/c/'
msys2_repository_url='https://github.com/igraph/igraph/'
msys2_documentation_url='https://igraph.org/c/#docs'
msys2_issue_tracker_url='https://github.com/igraph/igraph/issues/'
msys2_references=(
  'archlinux: igraph'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-glpk"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-plfit"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-arpack")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/igraph/igraph/releases/download/${pkgver}/igraph-${pkgver}.tar.gz"
        '0001-igraph-add-cflags-private-pkgconfig.patch')
sha256sums=('03ba01db0544c4e32e51ab66f2356a034394533f61b4e14d769b9bbf5ad5e52c'
            '07a1303781723b1e449899492431cb75156ac2b7450b48c8d28bd064b0703d35')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}/0001-igraph-add-cflags-private-pkgconfig.patch"
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  for libtype in 'static' 'shared'; do
    case $libtype in
      (static) _BUILD_SHARED_LIBS=OFF;;
      (shared) _BUILD_SHARED_LIBS=ON;;
      (*) exit 1
    esac

      MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      ${MINGW_PREFIX}/bin/cmake.exe \
        -GNinja \
        -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
        -DBUILD_SHARED_LIBS=${_BUILD_SHARED_LIBS} \
        ${_extra_config[@]} \
        -DBUILD_TESTING=OFF \
        -DIGRAPH_ENABLE_TLS=ON \
        -DIGRAPH_GLPK_SUPPORT=ON \
        -DIGRAPH_GRAPHML_SUPPORT=ON \
        -DIGRAPH_WARNINGS_AS_ERRORS=OFF \
        -S "${_realname}-${pkgver}" \
        -B "build-${MSYSTEM}-${libtype}"

      ${MINGW_PREFIX}/bin/cmake --build "build-${MSYSTEM}-${libtype}"
  done
}

check() {
  # igraph 0.9.4: All tests succeeded.
  for libtype in 'static' 'shared'; do
    ${MINGW_PREFIX}/bin/cmake -S "${_realname}-${pkgver}" -B "build-${MSYSTEM}-${libtype}" -DBUILD_TESTING=ON
    ${MINGW_PREFIX}/bin/cmake --build build-${MSYSTEM}-${libtype} --target build_tests
    ${MINGW_PREFIX}/bin/ctest --test-dir build-${MSYSTEM}-${libtype} --output-on-failure
  done
}

package() {
  # Install the libtypes one over another (until the simultaneous build of
  # shared and static libraries is implemented). :(
  for libtype in 'static' 'shared'; do
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install "build-${MSYSTEM}-${libtype}"
  done

  # Remove path reference in cmake
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/${_realname}/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
